<?php !defined( 'SUNCMS' ) && exit( 'No direct script access allowed'  );

/**
 * 
 * Sun CMS
 * 
 * @author			Burning
 * @copyright		Copyright (c) 2009, IsunCms.Com.
 * @license			http://www.isuncms.com/license.htm
 * @link				http://www.isuncms.com/
 * @since				Version 1.0 Beta
 * 
 */

class Member{

	var $db;
	
	function __construct()
	{
		global $db;
		$this->db = $db;
	}
	
	function get_member_no( $type, $id )
	{
		switch( $type )
		{
			case 'group':
				$member_no = $this->db->count( DB_PRE.'member', array( 'group_id' => $id ) );
				break;
			case 'all':
				$member_no = $this->db->count( DB_PRE.'member' );
				break;
			case 'validate':
				$member_no = $this->db->count( DB_PRE.'memberdata', array( 'validate' => $id ) );
				break;
		}
		return $member_no;
	}
	
	function get_member_list( $start, $type, $id )
	{
		switch( $type )
		{
			case 'group':
				$where = "WHERE `a`.`group_id`='$id'";
				break;
			case 'all':
				$where = '';
				break;
			case 'validate':
				$where = "WHERE `c`.`validate`='$id'";
				break;
		}
		$sql = "SELECT `a`.`id`, `a`.`username`, `b`.`name` AS `group`, `c`.`register_time`, `c`.`last_login_time`, `c`.`validate`, `c`.`ban_speak` FROM `".DB_PRE."member` AS `a` 
				LEFT JOIN `".DB_PRE."membergroup` AS `b` ON `a`.`group_id`=`b`.`id` LEFT JOIN `".DB_PRE."memberdata` AS `c` ON `a`.`id`=`c`.`uid` 
				$where ORDER BY `a`.`id` LIMIT $start, 20";
		$result = $this->db->query( $sql );
		$members = array();
		foreach( $result as $v )
		{
			$members[] = array
			(
					'id' => $v['id'],
					'username' => $v['username'],
					'validate' => $v['validate'],
					'ban_speak' => $v['ban_speak'],
					'group' => $v['group'],
					'register_time' => substr( $v['register_time'], 0, 10 ),
					'last_login_time' => substr( $v['last_login_time'], 0, 10 )
			);
		}
		return $members;
	}
	
	function get_group_tree()
	{
		return cache_read( 'power', 'member_group' );
	}
	
	function add_member( $basic, $datas )
	{
		$basic['password'] = md5( $basic['password'] );
		$result_basic = $this->db->insert( DB_PRE.'member', $basic );
		if( $result_basic )
		{
			$id = $this->db->insert_id();
		}
		$datas['uid'] = $id;
		$result_datas = $this->db->insert( DB_PRE.'memberdata', $datas );
		return ( $result_basic && $result_datas )?true:false;
	}
	
	/**
	 * 删除用户
	 *
	 * @param string $ids
	 * @return bool
	 */
	function delete( $ids )
	{
		$id_arrays = explode( '_', $ids );
		$i = 0;
		foreach( $id_arrays as $v )
		{
			if( !empty( $v ) )
			{
				( $this->db->delete( DB_PRE.'member', array( 'id' => $v ) ) && $this->db->delete( DB_PRE.'memberdata', array( 'uid' => $v ) ) )?'':$i = 1;
			}
		}
		return ( $i == 0 )?true:false;
	}
	
	/**
	 * 禁言/审核用户
	 *
	 * @param string $ids
	 * @return bool
	 */
	function validate( $ids, $type, $word )
	{
		$id_arrays = explode( '_', $ids );
		$i = 0;
		foreach( $id_arrays as $v )
		{
			if( !empty( $v ) )
			{
				$this->db->update( DB_PRE.'memberdata', array( $word => $type ), array( 'uid' => $v ) )?'':$i = 1;
			}
		}
		return ( $i == 0 )?true:false;
	}
	
	/**
	 * 移动文章
	 *
	 * @param string $ids
	 * @param int $group_id
	 * @return bool
	 */
	function move( $ids, $group_id )
	{
		$id_arrays = explode( '_', $ids );
		$i = 0;
		foreach( $id_arrays as $v )
		{
			if( !empty( $v ) )
			{
				$this->db->update( DB_PRE.'member', array( 'group_id' => $group_id ), array( 'id' => $v ) )?'':$i = 1;
			}
		}
		return ( $i == 0 )?true:false;
	}
	
	/**
	 * 添加用户组
	 *
	 * @param array $datas
	 * @return int/bool
	 */
	function addgroup( $datas )
	{
		$default_power = $datas['type'] == 3?cache_read( 'power', 'default_system_power' ):cache_read( 'power', 'group_1' );
		$power = serialize( $default_power );
		$sql = "INSERT INTO `".DB_PRE."membergroup` ( `name`, `grade`, `upgrade_points`, `type`, `power` ) VALUES ( '{$datas['name']}', '{$datas['grade']}', '{$datas['upgrade_points']}', '{$datas['type']}', '$power' )";
		if( $this->db->execute( $sql ) )
		{
			$insert_id = $this->db->insert_id();
			cache_member_group();
			cache_power( $insert_id );
			return $insert_id;
		}
		return false;
	}
	
	/**
	 * 删除用户组
	 *
	 * @param int $id
	 * @return bool
	 */
	function delgroup( $id )
	{
		$this->db->update( DB_PRE.'member', array( 'group_id' => 1 ), array( 'group_id' => $id ) );
		$this->db->delete( DB_PRE.'membergroup', array( 'id' => $id ) );
		cache_member_group();
		@unlink( SUNCMS.'datas/cache/power/group_'.$id.'.php' );
		return true;
	}
	
	/**
	 * 获取用户详细
	 *
	 * @param int $id
	 * @return array
	 */
	function get_member( $id )
	{	
		$sql = "SELECT `a`.`group_id`, `a`.`username`, `b`.`email`, `b`.`validate`, `b`.`sex`, `b`.`birthday`, `b`.`qq`, `b`.`msn`, `b`.`integral`, `b`.`money`, `b`.`contribute`, `b`.`reputation`, `b`.`homepage`, `b`.`signature`, `b`.`face`, `b`.`introduce` FROM `".DB_PRE."member` AS `a` LEFT JOIN `".DB_PRE."memberdata` AS `b` ON `a`.`id` = `b`.`uid` WHERE `a`.`id` = $id";
		$member = $this->db->query( $sql, true );
		return $member;
	}
	
	/**
	 * 更新用户资料
	 *
	 * @param int $id
	 * @param array $basic
	 * @param array $datas
	 * @return bool
	 */
	function update_member( $id, $basic, $datas )
	{
		if( empty( $basic[password] ) )
		{
			unset( $basic['password'] );
		}
		else 
		{
			$basic['password'] = md5( $basic['password'] );
		}
		$a = $this->db->update( DB_PRE.'member', $basic, array( 'id' => $id ) );
		$b = $this->db->update( DB_PRE.'memberdata', $datas, array( 'id' => $id ) );
		return ( $a && $b )?true:false;
	}
	
	/**
	 * 搜素用户
	 *
	 * @param string $type
	 * @param string $value
	 */
	function search( $rule, $key )
	{
		$sql = "SELECT `a`.`group_id`, `a`.`username`, `b`.`email`, `b`.`validate`, `b`.`sex`, `b`.`birthday`, `b`.`qq`, `b`.`msn`, `b`.`integral`, `b`.`money`, `b`.`contribute`, `b`.`reputation`, `b`.`homepage`, `b`.`signature`, `b`.`face`, `b`.`introduce` FROM `".DB_PRE."member` AS `a` LEFT JOIN `".DB_PRE."memberdata` AS `b` ON `a`.`id` = `b`.`uid` WHERE `a`.`$rule` = '$key'";
		$member = $this->db->query( $sql, true );
		return $member;
	}
	
	/**
	 * 获取权限
	 *
	 * @param int $id
	 * @return array
	 */
	function get_power( $id )
	{
		$sql = "SELECT `upgrade_points`, `grade`, `name`, `power` FROM `".DB_PRE."membergroup` WHERE `id`=$id";
		$data = $this->db->query( $sql, true );
		$data['power'] = unserialize( $data['power'] );
		return $data;
	}
	
	/**
	 * 更新权限
	 *
	 * @param int $id
	 * @param array $data
	 * @return bool
	 */
	function update_group( $id, $data, $name, $grade, $upgrade_points )
	{
		$data = array
		(
				'power' => serialize( $data ),
				'name' => $name,
				'grade' => $grade,
				'upgrade_points' => $upgrade_points
		);
		if( $this->db->update( DB_PRE.'membergroup', $data, array( 'id' => $id ) ) )
		{
			cache_member_group();
			cache_power( $id );
			return true;
		}
		return false;
	}
	
	/**
	 * 成员统计
	 *
	 * @return array
	 */
	function get_group_stat()
	{
		$groups = cache_read( 'power', 'member_group' );
		$datas = array();
		foreach( $groups as $group )
		{
			foreach( $group as $v )
			{
				$datas[] = array
				(
						'id' => $v['id'],
						'name' => $v['name'],
						'no' => $this->db->count( DB_PRE.'member', array( 'group_id' => $v['id'] ) )
				);
			}
		}
		return $datas;
	}
	
	
	/**
	 * 更新用户组资料
	 *
	 * @param array $group
	 * @return bool
	 */
	function uptgpb( $group )
	{
		$i = 0;
		foreach( $group as $k => $v )
		{
			$this->db->update( DB_PRE.'membergroup', $v, array( 'id' => $k ) )?'':$i=1;
		}
		if( $i == 0 )
		{
			cache_member_group();
			return true;
		}
		return false;
	}
	
	
}